﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BHYT.Server.Entities;

namespace BHYT.Server.Bll
{
    public class NguoiDungBll
    {
        BHYTEntities entities;

        public NguoiDungBll()
        {
            entities = new BHYTEntities();
        }

        public NguoiDungEntities CreateUser(NguoiDungEntities nguoiDung)
        {
            NguoiDung nguoi = new NguoiDung();
            nguoi.TenNguoiDung = nguoiDung.TenNguoiDung;
            nguoi.TenDangNhap = nguoiDung.TenDangNhap;
            nguoi.MatKhau = nguoiDung.MatKhau;
            nguoi.Email = nguoiDung.Email;
            entities.AddToNguoiDungs(nguoi);
            if (entities.SaveChanges() > 0)
            { 
                nguoiDung.IdNguoiDung = nguoi.IdNguoiDung;
                return nguoiDung;
            }
            return null;
        }

        public NguoiDung ValidationUser(string tenDangNhap, string password)
        {
            NguoiDung nguoiDung = (from a in entities.NguoiDungs
                                      where a.TenDangNhap == tenDangNhap
                                       select a).FirstOrDefault();
            if (nguoiDung != null)
            {
                if (Password.ComparePasswords(nguoiDung.MatKhau, password))
                    return nguoiDung;
                return null;
            }
            return null;
        }

        public bool UpdateUser(NguoiDungEntities nguoiDung)
        {
            return true;
        }

        public bool DeleteUser(NguoiDungEntities nguoiDung)
        {
            return true;
        }

    }
}
